package com.zbs.simbot.data;

import com.alibaba.fastjson2.TypeReference;
import com.zbs.simbot.data.mysuser.MysActivateCookieGameRole;
import com.zbs.simbot.data.mysuser.MysCookie;
import com.zbs.simbot.data.mysuser.MysGameRole;
import com.zbs.simbot.data.mysuser.MysUid;
import com.zbs.simbot.utils.JsonFileUtil;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MysUserDataUtil {

    /**
     * 保存uid
     *
     * @param uid
     * @param qq
     * @return
     */
    public static MysUid saveUid(String uid, Long qq) {
        String uidFilePath = FilePathUtil.getUidFilePath(qq);
        MysUid mysUid = new MysUid(qq, uid);
        JsonFileUtil.saveJson(mysUid,uidFilePath);
        return mysUid;
    }

    /**
     * 获取绑定的UID
     *
     * @param qq
     * @return
     */
    public static MysUid getUid(Long qq) {
        String uidFilePath = FilePathUtil.getUidFilePath(qq);
        return JsonFileUtil.readJsonObjectFile(uidFilePath, MysUid.class);
    }

    /**
     * 保存当前激活cookie
     * @param qq qq
     * @param cookie cookie
     * @param gameRole gameRole
     * @return
     */
    public static MysActivateCookieGameRole saveActivateCookieGameRole(Long qq, MysCookie cookie, MysGameRole gameRole) {
        String activateCookieGameRoleFilePath = FilePathUtil.getActivateCookieGameRoleFilePath(qq);
        MysActivateCookieGameRole activate = new MysActivateCookieGameRole(cookie, gameRole);
        JsonFileUtil.saveJson(activate,activateCookieGameRoleFilePath);
        return activate;
    }

    /**
     * 获取当前激活cookie
     * @param qq
     * @return
     */
    public static MysActivateCookieGameRole getActivateCookieGameRole(Long qq) {
        String activateCookieGameRoleFilePath = FilePathUtil.getActivateCookieGameRoleFilePath(qq);
        return JsonFileUtil.readJsonObjectFile(activateCookieGameRoleFilePath,MysActivateCookieGameRole.class);
    }


    /**
     * 保存通行证下的角色
     * @param qq QQ
     * @param cookie cookie
     * @param gameRoleList 游戏角色
     * @return key: tuid 通行证ID ，value： MysGameRole 米游社角色
     */
    public static Map<String, Map<String, MysGameRole> > saveGameRoles(Long qq, MysCookie cookie, List<MysGameRole> gameRoleList) {
        String gameRoleFilePath = FilePathUtil.getGameRoleFilePath(qq);
        Map<String, Map<String, MysGameRole>> mysGameRoles = getGameRoles(qq);
        if (CollectionUtils.isEmpty(mysGameRoles)) {
            mysGameRoles = new HashMap<>();
        }
        mysGameRoles.put(cookie.getTuid(),gameRoleList.stream().collect(Collectors.toMap(MysGameRole::getGame_uid,gameRole -> gameRole)));
        JsonFileUtil.saveJson(mysGameRoles,gameRoleFilePath);
        return mysGameRoles;
    }

    /**
     * 获取 QQ 下的所有通行证的游戏角色
     * @param qq qq
     * @return key: tuid 通行证ID ，value： MysGameRole 米游社角色
     */
    public static Map<String, Map<String, MysGameRole>> getGameRoles(Long qq) {
        String gameRoleFilePath = FilePathUtil.getGameRoleFilePath(qq);
        return JsonFileUtil.readJsonObjectFile(gameRoleFilePath, new TypeReference<>() {});
    }

    /**
     * 获取 QQ 下的指定通行证的游戏角色
     * @param qq qq
     * @param tuid  tuid 通行证ID
     * @return 游戏角色
     */
    public static List<MysGameRole> getGameRolesByTuid(Long qq,String tuid) {
        Map<String, Map<String, MysGameRole>> gameRoles = getGameRoles(qq);
        if (CollectionUtils.isEmpty(gameRoles)) {
            return null;
        }
        if (!gameRoles.containsKey(tuid)) {
            return null;
        }
        return gameRoles.get(tuid).values().stream().toList();
    }

    /**
     * 获取 QQ 下的指定通行证的游戏角色
     * @param qq qq
     * @param tuid  tuid 通行证ID
     * @return 游戏角色
     */
    public static MysGameRole getGameRolesByTuid(Long qq,String tuid,String uid) {
        Map<String, Map<String, MysGameRole>> gameRoles = getGameRoles(qq);
        if (CollectionUtils.isEmpty(gameRoles)) {
            return null;
        }

        if (!gameRoles.containsKey(tuid)) {
            return null;
        }
        return gameRoles.get(tuid).get(uid);
    }

    /**
     * 保存cookie
     * @param qq qq
     * @param cookie cookie
     * @return key: tuid 通行证ID ，value： MysCookie 米游社cookie
     */
    public static Map<String, MysCookie> saveCookie(Long qq, MysCookie cookie) {
        String cookieFilePath = FilePathUtil.getCookieFilePath(qq);
        Map<String, MysCookie> mysCookieMap = getCookieList(qq);
        if (CollectionUtils.isEmpty(mysCookieMap)) {
            // 如果绑定的cookie是空的，则新建一个
            mysCookieMap = new HashMap<>();
        }
        cookie.setQq(qq);
        mysCookieMap.put(cookie.getTuid(),cookie);
        JsonFileUtil.saveJson(mysCookieMap,cookieFilePath);
        return mysCookieMap;
    }

    /**
     * 获取绑定的cookie列表
     *
     * @param qq qq
     * @return key: tuid 通行证ID ，value： MysCookie 米游社cookie
     */
    public static Map<String, MysCookie> getCookieList(Long qq) {
        String cookieFilePath = FilePathUtil.getCookieFilePath(qq);
        return JsonFileUtil.readJsonObjectFile(cookieFilePath, new TypeReference<>() {});
    }


}
